

package com.bei_en.teacher.admin.controller.education;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.education.bean.Education;
import com.bei_en.teacher.admin.model.education.condition.EducationCondition;
import com.bei_en.teacher.admin.model.education.vo.EducationVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.education.EducationService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-9-25 10:06:59
 */
@Controller
@RequestMapping("/api/Education")
public class EducationAPPController extends BaseController {

    @Autowired
    EducationService educationService;
    @RequestMapping("/education")
	@ResponseBody
    public JsonResult education(Model model,@Valid  EducationCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<EducationVo> list =new ArrayList<EducationVo>();
		   list = educationService.findEducationPage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   EducationVo educationVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   educationVo=list.get(i);
	   seetingJsonObject(object,"Id",  educationVo.getId());
	   seetingJsonObject(object,"name",  educationVo.getName());
	   seetingJsonObject(object,"parent",  educationVo.getParent());
	   seetingJsonObject(object,"createTime",  educationVo.getCreateTime());
	   seetingJsonObject(object,"provinceId",  educationVo.getProvinceId());
	   seetingJsonObject(object,"cityId",  educationVo.getCityId());
	   seetingJsonObject(object,"areaId",  educationVo.getAreaId());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/educationAdd")
    @ResponseBody
    public JsonResult educationAdd(Model model,@Valid  EducationCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		
      	
		    EducationVo education=new EducationVo();
		    
	   education.setId(vo.getId());
	   education.setName(vo.getName());
	   education.setParent(vo.getParent());
	   education.setCreateTime(new Date(vo.getCreateTime()));
	   education.setProvinceId(vo.getProvinceId());
	   education.setCityId(vo.getCityId());
	   education.setAreaId(vo.getAreaId());
		    
			educationService.insert(education);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
    @RequestMapping("/educationEdit")
    public JsonResult educationEdit(Model model,@Valid  EducationCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	EducationVo education=new EducationVo();
	
		educationService.updateSelective(education);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
